package com.zbkj.crmeb.order.controller;

import java.util.List;

import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.order.StoreOrderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zbkj.crmeb.order.request.TmallOrderInfoRequest;
import com.zbkj.crmeb.order.request.TmallOrderParamRequest;
import com.zbkj.crmeb.order.request.TmallStoreOrderRequest;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.service.StoreOrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/store/order")
@Api(tags = "第三方订单推送") //配合swagger使用
public class TmallStoreOrderController {
    @Autowired
    private StoreOrderService storeOrderService;
    @Autowired
    private StoreOrderFactory storeOrderFactory;

    /**
     * 第三方订单推送
     * @author
     * @since
     */
    @ApiOperation(value = "第三方订单推送")
    @RequestMapping(value = "/push", method = RequestMethod.POST)
    public CommonResult<Boolean> push(@RequestBody TmallOrderParamRequest tmallOrderParamRequest){
        log.info("--------------- 收到天猫推送订单: {} ---------------", tmallOrderParamRequest);
        TmallStoreOrderRequest order = tmallOrderParamRequest.getOrder();
        List<TmallOrderInfoRequest> orderInfoRequestList = tmallOrderParamRequest.getOrderInfoRequestList();
        
        StoreOrder storeOrder = storeOrderService.getByOrderId(order.getOrderId());
        if(null != storeOrder){
            log.info("--------------- 推送第三方订单已经存在,orderId:{} ---------------",order.getOrderId());
            return CommonResult.failed("订单已经存在");
        }
        storeOrderService.push(order,orderInfoRequestList);
        return CommonResult.success();
    }

    /**
     * 第三方订单取消
     * @author
     * @since
     */
    @ApiOperation(value = "第三方订单取消")
    @RequestMapping(value = "/cancel", method = RequestMethod.GET)
    public CommonResult<Boolean> cancel(TmallStoreOrderRequest request){
        log.info("--------------- 收到天猫取消订单-orderId: {} ---------------", request.getOrderId());

        StoreOrder storeOrder = storeOrderService.getByOrderId(request.getOrderId());
        if(null == storeOrder){
            log.info("--------------- 推送第三方订单不存在,orderId:{} ---------------",request.getOrderId());
            return CommonResult.failed("订单不存在");
        }
        if(1 !=storeOrder.getPushStatus()){
            log.info("--------------- 推送第三方订单没有推送成功不能取消,orderId:{} ---------------",request.getOrderId());
            return CommonResult.failed("订单没有推送成功");
        }
        storeOrderFactory.cancelOrder(storeOrder);
        return CommonResult.success();
    }
}
